package di.ass.main;

import di.ass.database.DatabaseHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import di.ass.R;

public class ShoppingListActivity extends Activity implements OnClickListener {
	private ImageButton start, help, setting, exit;
	private ImageView imageView;
	DatabaseHandler db;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initialyze();
	}

	public void initialyze() {
		start = (ImageButton) findViewById(R.id.start);
		help = (ImageButton) findViewById(R.id.help);
		setting = (ImageButton) findViewById(R.id.setting);
		exit = (ImageButton) findViewById(R.id.exit);
		imageView = (ImageView) findViewById(R.id.titleMainImageView);
		start.setOnClickListener(this);
		help.setOnClickListener(this);
		setting.setOnClickListener(this);
		exit.setOnClickListener(this);
		db = new DatabaseHandler(this);
		db.addLanguage("Việt Nam", 1);
		db.addLanguage("English", 0);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (db.getLanguageUse().equals("Việt Nam")) {
			imageView.setImageResource(R.drawable.title_main_vi);
		} else if (db.getLanguageUse().equals("English")) {
			imageView.setImageResource(R.drawable.title_main_eng);
		}
	}

	@Override
	public void onClick(View arg0) {

		// TODO Auto-generated method stub
		Animation animation = AnimationUtils.loadAnimation(this,
				R.anim.btn_style);
		switch (arg0.getId()) {
		case R.id.start:
			start.startAnimation(animation);
			Intent intentView = new Intent("di.ass.view.ListFormula");
			this.startActivity(intentView);
			break;
		case R.id.help:
			help.startAnimation(animation);
			Intent b = new Intent("di.ass.main.HELP");
			startActivity(b);
			break;
		case R.id.setting:
			setting.startAnimation(animation);
			String[] strSetting = null;
			if (db.getLanguageUse().equals("Việt Nam")) {
				strSetting = getResources().getStringArray(R.array.title_vi);
			} else if (db.getLanguageUse().equals("English")) {
				strSetting = getResources().getStringArray(R.array.title_eng);
			}
			final String[] lang = strSetting;
			final CharSequence[] items = { strSetting[3], strSetting[4] };
			AlertDialog.Builder builderset = new AlertDialog.Builder(this);
			builderset.setTitle(strSetting[0]);
			builderset.setItems(items, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialogInterface, int item) {
					if (items[item].toString().equals(lang[3])) {
						db.updateLang("Việt Nam");
						imageView.setImageResource(R.drawable.title_main_vi);
					} else if (items[item].toString().equals(lang[4])) {
						db.updateLang("English");
						imageView.setImageResource(R.drawable.title_main_eng);
					}
					return;
				}
			});
			builderset.create().show();
			break;
		case R.id.exit:
			exit.startAnimation(animation);
			String[] textExit = null;
			if (db.getLanguageUse().equals("Việt Nam")) {
				textExit = getResources().getStringArray(R.array.quit_vi);
			} else if (db.getLanguageUse().equals("English")) {
				textExit = getResources().getStringArray(R.array.quit_eng);
			}
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(textExit[0]);
			builder.setMessage(textExit[1]);
			builder.setPositiveButton(textExit[2],
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setNeutralButton(textExit[3],
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub

						}
					});
			builder.setIcon(R.drawable.info);
			builder.show();
			break;
		}
	}
}
